package Gui;

import Silnik.MyDate;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * A simple demonstration application showing how to create an area chart using data from a
 * {@link CategoryDataset}.
 */
public class WykresWalut
{

   /**
    * Rysowanie wykresu dla zadanych danych
    */
   private double[] data;
   private JFreeChart chart;
   private final DefaultCategoryDataset dataset;
   private ChartPanel chartPanel;
   public WykresWalut()
   {
      dataset = new DefaultCategoryDataset();
   }
   
   public JPanel rysujWykres(ArrayList<Double> wartosci,ArrayList<MyDate> daty,String waluta )
   {
      assert wartosci.size() == daty.size();
      dataset.clear();
      for (int i = 0; i < wartosci.size(); i++)
      {
         dataset.addValue(wartosci.get(i), waluta, daty.get(i));
      }
      chart = createChart(dataset);
      chartPanel = new ChartPanel(chart, true, true, true, true, true);
      chartPanel.setPreferredSize(new Dimension(590, 590));
      chartPanel.setEnforceFileExtensions(false);
      chartPanel.setBorder(BorderFactory.createCompoundBorder(
              BorderFactory.createEtchedBorder(EtchedBorder.RAISED),
              BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)));
      return chartPanel;
      
   }

   // ****************************************************************************
   // * JFREECHART DEVELOPER GUIDE                                               *
   // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
   // * to purchase from Object Refinery Limited:                                *
   // *                                                                          *
   // * http://www.object-refinery.com/jfreechart/guide.html                     *
   // *                                                                          *
   // * Sales are used to provide funding for the JFreeChart project - please    * 
   // * support us so that we can continue developing free software.             *
   // ****************************************************************************
   /**
    * Creates a chart.
    *
    * @param dataset the dataset.
    *
    * @return The chart.
    */
   private JFreeChart createChart(final CategoryDataset dataset)
   {

      final JFreeChart chartL = ChartFactory.createLineChart(
              "Kursy walut", // chart title
              "Data", // domain axis label
              "Kurs", // range axis label
              dataset, // data
              PlotOrientation.VERTICAL, // orientation
              true, // include legend
              true, // tooltips
              false // urls
              );

      // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

      // set the background color for the chart...
//        final StandardLegend legend = (StandardLegend) chart.getLegend();
      //      legend.setAnchor(StandardLegend.SOUTH);

      chartL.setBackgroundPaint(Color.white);

      final CategoryPlot plot = chartL.getCategoryPlot();
      plot.setForegroundAlpha(0.5f);

      plot.setBackgroundPaint(Color.lightGray);
      plot.setDomainGridlinesVisible(true);
      plot.setDomainGridlinePaint(Color.white);
      plot.setRangeGridlinesVisible(true);
      plot.setRangeGridlinePaint(Color.white);

      final CategoryAxis domainAxis = plot.getDomainAxis();
      domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
      domainAxis.addCategoryLabelToolTip("Kurs", "Kurs czegoś");

      final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
      rangeAxis.setAutoRange(true);
      rangeAxis.configure();
      
      return chartL;

   }
}